package com.kolon.business.mKolonInt.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mKolonInt.controller.MKolonIntComentModifyController")
public class MKolonIntComentModifyController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mKolonInt/mKolonIntComentModify.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 도메인
		String url = MsgUtil.getMessage("xml.url");
		// 세션 아이디 및 암호 값을 불러옴
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String id = (String)userMap.get("userId");
		String pw = (String)userMap.get("userPw");
		String CompanyCode = (String)userMap.get("CompanyCD");
		String returnUrl = "/business/mKolonInt/mKolonIntComent.do?ArticleCD=" + request.getParameter("ArticleCD");
		ModelAndView modelAndView = new ModelAndView();
		if(!"".equals(request.getParameter("Contents")) && request.getParameter("Contents")!=null) {
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			buffer.append("/kolon_int.aspx?strMode=").append("11");
			buffer.append("&CommentCD=").append(request.getParameter("CommentCD"));
			buffer.append("&Contents=").append(ComUtil.getEncode(request.getParameter("Contents"),"EUC-KR"));
			buffer.append("&TxtEmoID=").append("1");
			buffer.append("&userid=").append(id);
			buffer.append("&pw=").append(pw);
			// XML DOCUMENT 및 인증 처리
			ComUtil.getDocument(buffer.toString(), id, pw);
			modelAndView.addObject("message", "수정되었습니다.");
			modelAndView.addObject("scriptType", "003");
			modelAndView.addObject("message_url", returnUrl);
			modelAndView.setViewName("/business/common/alert");
		}else{
			// URL 생성
			StringBuffer buffer = new StringBuffer();
			buffer.append(url);
			buffer.append("/kolon_int.aspx?strMode=").append("4");
			buffer.append("&ArticleCD=").append(request.getParameter("ArticleCD"));
			buffer.append("&userid=").append(id);
			buffer.append("&pw=").append(pw);
			// XML DOCUMENT 및 인증 처리
			Document doc = ComUtil.getDocument(buffer.toString(), id, pw);
			Element element = doc.getRootElement();
			List xmlList = element.getChildren("BoardDetail");
			Iterator itXml = xmlList.iterator();
			Map map = new HashMap();
			// XML 데이터 
			if (itXml.hasNext()) {
				Element el = (Element)itXml.next();
				map = new HashMap();
				map.put("ArticleCD", el.getChild("ArticleCD").getText());
				map.put("UserID", el.getChild("UserID").getText());
				map.put("UserName", el.getChild("UserName").getText());
				map.put("Title", el.getChild("Title").getText());
				map.put("Contents", el.getChild("Contents").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
				map.put("CreateDate", el.getChild("CreateDate").getText().substring(0, 10));
				map.put("ExpireDate", el.getChild("ExpireDate").getText());
				map.put("ViewCnt", ComUtil.addComma(el.getChild("ViewCnt").getText()));
				map.put("FileOnly", el.getChild("FileOnly").getText());
				map.put("Importance", el.getChild("Importance").getText());
				map.put("BoardName", el.getChild("BoardName").getText());
				map.put("IsNotice", el.getChild("IsNotice").getText());
				map.put("B_ACL", el.getChild("B_ACL").getText());
				map.put("BoardCD", el.getChild("BoardCD").getText());
				map.put("SAdmin", el.getChild("SAdmin").getText());
				map.put("PublicCanWrite", el.getChild("PublicCanWrite").getText());
				map.put("searchauth", el.getChild("searchauth").getText());
				map.put("Gubun", el.getChild("Gubun").getText());
				map.put("Company", el.getChild("Company").getText());
				map.put("Price", el.getChild("Price").getText());
				map.put("Area", el.getChild("Area").getText());
				map.put("Status", el.getChild("Status").getText());
				map.put("Phone", el.getChild("Phone").getText());
				map.put("Address", el.getChild("Address").getText());
				map.put("Theme", el.getChild("Theme").getText());
				map.put("Point", el.getChild("Point").getText());
				map.put("NickName", el.getChild("NickName").getText());
				map.put("Parking", el.getChild("Parking").getText());
				map.put("Ref_Seq", el.getChild("Ref_Seq").getText());			
			    map.put("UCStatus", el.getChild("UCStatus").getText());
				map.put("TelOffice", el.getChild("TelOffice").getText());
				map.put("Mobile", el.getChild("Mobile").getText());
			}
			modelAndView.addObject("viewMap", map);
			modelAndView.addObject("url", url);
			modelAndView.addObject("CommentCD", request.getParameter("CommentCD"));
			modelAndView.addObject("AContents", ComUtil.getDecode(request.getParameter("AContents"),"UTF-8"));
		}
		
		return modelAndView;
	}

}